<template>
  <a-card :style="cardStyle" :bordered="false">
    <a-row :gutter="24">
      <a-col :md="12">
        <a-spin :spinning="confirmLoading">
          <a-form :form="form" id="systemConfigModal">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.corporateName')">
              <a-input :placeholder="$t('systemManagement.pleaseCompanyName')" v-decorator.trim="[ 'companyName', validatorRules.companyName]" />
            </a-form-item>
            <!-- 公司税号 -->
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.companyDutyParagraph')">
              <a-input :placeholder="$t('systemManagement.pleaseEnterTheCompanyDutyParagraph')" v-decorator.trim="[ 'companyFax',validatorRules.companyFax]" />
            </a-form-item>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('basicInformation.taxRate')">
              <a-input :placeholder="$t('basicInformation.taxRate')" v-decorator.trim="[ 'companyContacts',validatorRules.companyContacts ]" />
            </a-form-item>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.companyAddress')">
              <a-input :placeholder="$t('systemManagement.pleaseCompanyAddress')" v-decorator.trim="[ 'companyAddress' ]" />
            </a-form-item>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.companyTelephone')">
              <a-input :placeholder="$t('systemManagement.pleaseCompanyPhone')" v-decorator.trim="[ 'companyTel' ]" />
            </a-form-item>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.companyZipCode')">
              <a-input :placeholder="$t('systemManagement.pleaseEnterTheCompanyZipCode')" v-decorator.trim="[ 'companyPostCode' ]" />
            </a-form-item>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.salesAgreement')">
              <a-input :placeholder="$t('systemManagement.operationTime')" v-decorator.trim="[ 'saleAgreement', validatorRules.saleAgreement ]" />
            </a-form-item>
            <!-- <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.unitFlag')">
              <a-select :placeholder="$t('systemManagement.pleaseUnitFlag')" v-decorator="[ 'unitFlag', validatorRules.unitFlag]">
                <a-select-option :value="'1'">基本单位</a-select-option>
                <a-select-option :value="'2'">大单位</a-select-option>
              </a-select>
            </a-form-item> -->
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.warehouseAuthority')">
              <a-switch :checked-children="$t('systemManagement.enable')" :un-checked-children="$t('systemManagement.close')" v-model="depotFlagSwitch" @change="onDepotChange"></a-switch>
              {{$t('systemManagement.ifWarehouses')}}
            </a-form-item>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.customerAuthority')">
              <a-switch :checked-children="$t('systemManagement.enable')" :un-checked-children="$t('systemManagement.close')" v-model="customerFlagSwitch" @change="onCustomerChange"></a-switch>
              {{$t('systemManagement.ifAllocate')}}
            </a-form-item>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.negativeInventory')">
              <a-switch :checked-children="$t('systemManagement.enable')" :un-checked-children="$t('systemManagement.close')" v-model="minusStockFlagSwitch" @change="onMinusStockChange"></a-switch>
              {{$t('systemManagement.ifNegative')}}
            </a-form-item>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('systemManagement.orderBySale')">
              <a-switch :checked-children="$t('systemManagement.enable')" :un-checked-children="$t('systemManagement.close')" v-model="purchaseBySaleFlagSwitch" @change="onPurchaseBySaleChange"></a-switch>
              {{$t('systemManagement.ifPurchase')}}
            </a-form-item>
          </a-form>
        </a-spin>
      </a-col>
    </a-row>
    <a-row :gutter="24">
      <a-col :md="12" align="middle">
        <a-button type="primary" @click="handleOk" :disabled="confirmLoading">{{$t('systemManagement.save')}}</a-button>
        <a-button style="margin-left:20px" @click="handleReset">{{$t('systemManagement.reset')}}</a-button>
      </a-col>
    </a-row>
  </a-card>
</template>
<!-- b y 7 5 2 7  1 8 9 2 0 -->
<script>
  import pick from 'lodash.pick'
  import {addSystemConfig,editSystemConfig,checkSystemConfig } from '@/api/api'
  import {autoJumpNextInput} from "@/utils/util"
  import {getAction } from '@/api/manage'
  import {mixinDevice} from '@/utils/mixin.js'
  export default {
    name: "SystemConfigList",
    mixins: [mixinDevice],
    data () {
      return {
        title:this.$t('systemManagement.operation'),
        cardStyle: '',
        visible: true,
        model: {},
        depotFlagSwitch: false, //仓库权限状态
        customerFlagSwitch: false, //客户权限状态
        minusStockFlagSwitch: false, //负库存状态
        purchaseBySaleFlagSwitch: false, //以销定购状态
        isReadOnly: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          companyName:{
            rules: [
              { required: true, message:this.$t('systemManagement.pleaseCompanyName') +'!' },
              { min: 2, max: 30, message:this.$t('systemManagement.length2and30'), trigger: 'blur' }
            ]
          },
          saleAgreement:{
            rules: [
              { min: 2, max: 200, message:this.$t('systemManagement.length2and200'), trigger: 'blur' }
            ]
          },
          unitFlag:{
            rules: [
              { required: true, message:this.$t('systemManagement.pleaseUnitFlag'), trigger: 'blur' }
            ]
          },
          companyContacts:{
            rules: [
              { required: true, message:this.$t('inPrint.pleaseTax'), trigger: 'blur' }
            ]
          },
          companyFax:{
            rules: [
              { required: true, message:this.$t('systemManagement.pleaseEnterTheCompanyDutyParagraph')+ '!'}
            ]
          }
        }
      }
    },
    created () {
      this.init()
      if(this.isDesktop()) {
        this.cardStyle = 'height:' + (document.documentElement.clientHeight-125) + 'px'
      }
    },
    methods: {
      onDepotChange(checked) {
        this.model.depotFlag = checked?'1':'0'
      },
      onCustomerChange(checked) {
        this.model.customerFlag = checked?'1':'0'
      },
      onMinusStockChange(checked) {
        this.model.minusStockFlag = checked?'1':'0'
      },
      onPurchaseBySaleChange(checked) {
        this.model.purchaseBySaleFlag = checked?'1':'0'
      },
      init () {
        let param = {
          search: {"companyName":""},
          currentPage: 1,
          pageSize: 10
        }
        getAction('/systemConfig/list', param).then((res)=>{
          if(res.code === 200){
            let record = res.data.rows[0]
            this.form.resetFields();
            this.model = Object.assign({}, record);
            this.visible = true;
            this.$nextTick(() => {
              this.form.setFieldsValue(pick(this.model,'companyName', 'companyContacts', 'companyAddress',
                'companyTel', 'companyFax', 'companyPostCode', 'saleAgreement', 'unitFlag', 'depotFlag', 'customerFlag',
                'minusStockFlag', 'purchaseBySaleFlag'))
              autoJumpNextInput('systemConfigModal')
            });
            if(record.id) {
              if (record.depotFlag != null) {
                this.depotFlagSwitch = record.depotFlag == '1' ? true : false;
              }
              if (record.customerFlag != null) {
                this.customerFlagSwitch = record.customerFlag == '1' ? true : false;
              }
              if (record.minusStockFlag != null) {
                this.minusStockFlagSwitch = record.minusStockFlag == '1' ? true : false;
              }
              if (record.purchaseBySaleFlag != null) {
                this.purchaseBySaleFlagSwitch = record.purchaseBySaleFlag == '1' ? true : false;
              }
            }
          } else {
            this.$message.info(res.data);
          }
        })
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            let obj;
            if(!this.model.id){
              obj=addSystemConfig(formData);
            }else{
              obj=editSystemConfig(formData);
            }
            obj.then((res)=>{
              if(res.code === 200){
                this.init()
                that.$message.info(this.$t('commodityManagement.savedSuccessfully')+'!');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      handleReset () {
        this.form.resetFields();
        this.depotFlagSwitch = false
        this.customerFlagSwitch = false
        this.minusStockFlagSwitch = false
        this.purchaseBySaleFlagSwitch = false
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>